package com.ly.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ly.mapper.StudentMapper;
import com.ly.pojo.Student;
import com.ly.pojo.bo.StudentBO;
import com.ly.service.StudentService;
import com.ly.utils.Identities;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional(propagation = Propagation.REQUIRED)
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void saveStudent(StudentBO studentBO) {
        Student student = new Student();
        BeanUtils.copyProperties(studentBO,student);
        student.setId(Identities.uuid());
        studentMapper.insertSelective(student);
    }

    @Override
    public PageInfo<Student> queryByCondition(Student student, int page, int pageSize) {
        //分页查询
        PageHelper.startPage(page, pageSize);
        //复杂查询必须的这么写（判断时间>=、<=、模糊查询）
        /*Example example = new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name", student.getName());
        criteria.andEqualTo("sex", student.getSex());
        List<Student> studentList = studentMapper.selectByExample(example);*/
        //简单查询

        List<Student> studentList = studentMapper.select(student);
        PageInfo<Student> pageListInfo = new PageInfo<Student>(studentList);
        return pageListInfo;
    }

    /**
     * 自定义方法进行查询
     * @param student
     * @param page
     * @param pageSize
     * @return
     */
    public PageInfo<Student> queryCustomList(Student student, int page, int pageSize) {
        //分页查询
        PageHelper.startPage(page, pageSize);
        List<Student> studentList = studentMapper.queryCustomList(student);
        PageInfo<Student> pageListInfo = new PageInfo<Student>(studentList);
        return pageListInfo;
    }
}
